//
//  LoadingAnimalView.m
//  LoanTeam15
//
//  Created by gynjobs on 2017/8/17.
//  Copyright © 2017年 cangming. All rights reserved.
//

#import "LoadingAnimalView.h"


static float startAngel = 0;


static id getWithLoadImageResource(){
    
    static NSArray * imageNames = nil;
    imageNames = [NSArray arrayWithObjects:
                  @"loading001.png",@"loading002.png",@"loading003.png",@"loading004.png",@"loading005.png",
                  @"loading006.png",@"loading007.png",@"loading008.png",@"loading009.png",@"loading010.png",
                  @"loading011.png",@"loading012.png",@"loading013.png",@"loading014.png",@"loading015.png",
                  @"loading016.png",@"loading017.png",@"loading018.png",@"loading019.png",@"loading020.png",
                  @"loading021.png",@"loading022.png",@"loading023.png",@"loading024.png",
                  nil];
    return imageNames;
    
}


static id getWithBlueLoadingImageResource(){
    
    static NSArray * imageNames = nil;
    imageNames = [NSArray arrayWithObjects:
                  @"blue_loading001.png",@"blue_loading002.png",@"blue_loading003.png",@"blue_loading004.png",
                  @"blue_loading005.png",@"blue_loading006.png",@"blue_loading007.png",@"blue_loading008.png",
                  @"blue_loading009.png",@"blue_loading010.png",@"blue_loading011.png",@"blue_loading012.png",
                  @"blue_loading013.png",@"blue_loading014.png",@"blue_loading015.png",@"blue_loading016.png",
                  @"blue_loading017.png",@"blue_loading018.png",@"blue_loading019.png",@"blue_loading020.png",
                  @"blue_loading021.png",@"blue_loading022.png",@"blue_loading023.png",@"blue_loading024.png",
                  nil];
    return imageNames;
    
}


@interface LoadingAnimalView (){
    UIView * bgView;
    UIImageView * indicatorView;
    UILabel * tipLabel;
    
}

@end


@implementation LoadingAnimalView
@synthesize load_type;


+(void)showBlueLoadingAddedTo:(nonnull UIView *)view{
    
    [self showLoadingAddedTo:view withType:LOAD_BLUE_CIRCLE];
}
+(void)showGrayLoadingAddedTo:(nonnull UIView *)view{
    
    [self showLoadingAddedTo:view withType:LOAD_GRAY_CIRCLE];
}
+(void)showWhiteLoadingAddedTo:(nonnull UIView *)view{
    
    [self showLoadingAddedTo:view withType:LOAD_WHITE_CIRCLE];
}


+ (void)showSuccessLoadingAddedTo:(nonnull UIView *)view withBlock:(nullable void (^)(void))block{
    [self showImageTextLoadingAddedTo:view withType:LOAD_SUCCESS withBlock:block];
}

+ (void)showImageTextLoadingAddedTo:(nonnull UIView *)view withType:(LOAD_TYPE)type withBlock:(nullable void (^)(void))block{
    LoadingAnimalView *loadingView = [[self alloc] initWithView:view];
    loadingView.load_type = type;
    [loadingView setImageTextSubs];
    [view addSubview:loadingView];
    
    
    [UIView animateWithDuration:1.5 animations:^{
        loadingView.alpha = 0.0;
    } completion:^(BOOL finished) {
        [self hideLoadingForView:view];
        if (block) {
            block();
        }
    }];
    
}

+ (void)showLoadingAddedTo:(nonnull UIView *)view withType:(LOAD_TYPE)type{
    
    LoadingAnimalView *loadingView = [[self alloc] initWithView:view];
    loadingView.load_type = type;
    [loadingView setSubViews];
    [view addSubview:loadingView];
    
}
+ (BOOL)hideLoadingForView:(nonnull UIView *)view{
    
    LoadingAnimalView *loadingView = [self LoadingForView:view];
    if (loadingView != nil) {
        
        [loadingView removeFromSuperview];
        return YES;
    }
    return NO;
    
}
+ (nullable LoadingAnimalView *)LoadingForView:(nonnull UIView *)view{
    
    NSEnumerator *subviewsEnum = [view.subviews reverseObjectEnumerator];
    for (UIView *subview in subviewsEnum) {
        if ([subview isKindOfClass:self]) {
            return (LoadingAnimalView *)subview;
        }
    }
    return nil;
    
}

+ (void)showToastAddedTo:(UIView*)view withTitle:(NSString *)title withBlock:(void (^)(void))block{
    
    if(!title || [title length] == 0){
        
        return;
    }
    
    MBProgressHUD*  hud = [[MBProgressHUD alloc] initWithView:view];
    
    hud.contentColor = [UIColor whiteColor];
    hud.label.text = title;
    hud.mode = MBProgressHUDModeText;
    hud.bezelView.backgroundColor = [UIColor blackColor];
    hud.bezelView.alpha = 0.2;
    //[hud registerForKVO];
    
    [view addSubview:hud];
    
    //指定距离中心点的X轴和Y轴的位置，不指定则在屏幕中间显示
    //    HUD.yOffset = 100.0f;
    //    HUD.xOffset = 100.0f;
    
    [hud showAnimated:YES whileExecutingBlock:^{
        sleep(1);
    } completionBlock:^{
        [hud removeFromSuperview];
        
        if(block){
            GYNLog(@"%@",hud);
            block();
        }
    }];
    
    //return MB_AUTORELEASE(hud);
}


-(void)dealloc{
    
    startAngel = 0;
    if(indicatorView){
        [indicatorView stopAnimating];
    }
    indicatorView = nil;
    
}

- (instancetype)initWithFrame:(CGRect)frame {
    if ((self = [super initWithFrame:frame])) {
        
        load_type = 0;
        [self commonInit];
    }
    return self;
}

- (id)initWithView:(UIView *)view {
    
    return [self initWithFrame:view.bounds];
}

- (void)commonInit {
    // Transparent background
    self.opaque = NO;
    self.backgroundColor = [UIColor clearColor];
    // Make it invisible for now
    //self.alpha = 0.0f;
    self.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
    self.layer.allowsGroupOpacity = NO;
    //[self setSubViews];
}

-(void)setSubViews{
    
    bgView = [[UIView alloc] init];
    bgView.opaque = NO;
    bgView.layer.cornerRadius = 6.0;
    [self addSubview:bgView];
    bgView.backgroundColor = [UIColor whiteColor];
    indicatorView = [[UIImageView alloc] init];
    [bgView addSubview:indicatorView];
    
    tipLabel = [[UILabel alloc] init];
    tipLabel.text = @"加载中...";
    tipLabel.font = [UIFont systemFontOfSize:14];
    tipLabel.textColor = [UIColor whiteColor];
    [bgView addSubview:tipLabel];
    [self initTypeView];
    
    [bgView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.height.mas_equalTo(65);
        make.width.mas_equalTo(150);
        make.centerX.equalTo(self.mas_centerX);
        make.centerY.equalTo(self.mas_centerY);
        
    }];
    
    [indicatorView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.equalTo(bgView.mas_left).with.offset(30);
        make.width.mas_equalTo(20);
        make.height.mas_equalTo(20);
        make.centerY.equalTo(self.mas_centerY);
    }];
    
    [tipLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.centerY.equalTo(bgView.mas_centerY);
        make.left.equalTo(indicatorView.mas_right).with.offset(15);
        
    }];
    
    //    [self startAnimation];
    
    
}

- (void)setImageTextSubs{
    bgView = [[UIView alloc] init];
    bgView.opaque = NO;
    bgView.layer.cornerRadius = 6.0;
    [self addSubview:bgView];
    
    indicatorView = [[UIImageView alloc] init];
    [bgView addSubview:indicatorView];
    
    tipLabel = [[UILabel alloc] init];
    tipLabel.text = @"导入成功";
    tipLabel.font = [UIFont systemFontOfSize:15];
    tipLabel.textColor = [UIColor colorWithString:@"ffffff"];
    [bgView addSubview:tipLabel];
    [self initTypeView];
    
    
    [bgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(100);
        make.width.mas_equalTo(125);
        make.centerX.equalTo(self.mas_centerX);
        make.centerY.equalTo(self.mas_centerY);
    }];
    
    [indicatorView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(bgView);
        make.top.equalTo(bgView).offset(16);
        make.width.height.mas_equalTo(40);
    }];
    
    [tipLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(indicatorView);
        make.top.equalTo(indicatorView.mas_bottom).offset(14);
        make.bottom.equalTo(bgView).offset(-16);
    }];
}



- (void)startAnimation
{
    CGAffineTransform endAngle = CGAffineTransformMakeRotation(startAngel * (M_PI / 180.0f));
    
    [UIView animateWithDuration:0.1 delay:0 options:UIViewAnimationOptionCurveLinear animations:^{
        indicatorView.transform = endAngle;
    } completion:^(BOOL finished) {
        startAngel += 10; [self startAnimation];
    }];
    
}

- (void)initTypeImageText{
    
}

-(void)initTypeView{
    
    switch (self.load_type) {
        case LOAD_BLUE_CIRCLE:{
            
            //            indicatorView.image = [UIImage imageNamed:@"loading_blue_circle"];
            bgView.backgroundColor = [UIColor clearColor];
            tipLabel.textColor = [UIColor colorWithString:@"#2F70B7"];
            [indicatorView setAnimationImages:[[ self class] genBlueImages]];
            [indicatorView startAnimating];
            
        }
            
            break;
        case LOAD_GRAY_CIRCLE:{
            
            bgView.backgroundColor = [UIColor blackColor];
            bgView.alpha = 0.5;
            indicatorView.image = [UIImage imageNamed:@"loading_gray_circle"];
        }
            break;
        case LOAD_WHITE_CIRCLE:{
            
            bgView.backgroundColor = [UIColor blackColor];
            bgView.alpha = 0.5;
            //            indicatorView.image = [UIImage imageNamed:@"loading_white_circle"];
            
            [indicatorView setAnimationImages:[[self class] genGrayImages]];
            [indicatorView startAnimating];
        }
            break;
        case LOAD_SUCCESS:{
            bgView.backgroundColor = [UIColor blackColor];
            bgView.alpha = 0.7;
            
            indicatorView.image = [UIImage imageNamed:@"loading_succeed"];
        }
        default:
            break;
    }
    
    
}


+(id)genGrayImages{
    
    NSArray * imageNames = getWithLoadImageResource();
    NSMutableArray * imageArray = [NSMutableArray arrayWithCapacity:2];
    
    for (NSInteger i = 0; i < [imageNames count]; i ++) {
        
        NSString * imageName = [imageNames objectAtIndex:i];
        UIImage * image = [UIImage imageNamed:imageName];
        [imageArray addObject:image];
    }
    
    return imageArray;
}

+(id)genBlueImages{
    
    NSArray * imageNames = getWithBlueLoadingImageResource();
    NSMutableArray * imageArray = [NSMutableArray arrayWithCapacity:2];
    
    for (NSInteger i = 0; i < [imageNames count]; i ++) {
        
        NSString * imageName = [imageNames objectAtIndex:i];
        UIImage * image = [UIImage imageNamed:imageName];
        [imageArray addObject:image];
    }
    
    return imageArray;
    
    
}
@end
